home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / mozilla-firefox / include / htmlparser / nsToken.h < prev   
C/C++ Source or Header  |  2006-05-08  |  9KB  |  308 lines

  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  26.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37.  
  38.  
  39. /**
  40.  * MODULE NOTES:
  41.  * @update  gess 4/1/98
  42.  * 
  43.  * This class is defines the basic notion of a token
  44.  * within our system. All other tokens are derived from
  45.  * this one. It offers a few basic interfaces, but the
  46.  * most important is consume(). The consume() method gets
  47.  * called during the tokenization process when an instance
  48.  * of that particular token type gets detected in the 
  49.  * input stream.
  50.  *
  51.  * CToken objects that are allocated from the heap _must_ be allocated
  52.  * using the nsTokenAllocator: the nsTokenAllocator object uses an
  53.  * arena to manage the tokens.
  54.  *
  55.  * The nsTokenAllocator object's arena implementation requires
  56.  * object size at destruction time to properly recycle the object;
  57.  * therefore, CToken::operator delete() is not public. Instead,
  58.  * heap-allocated tokens should be destroyed using the static
  59.  * Destroy() method, which accepts a token and the arena from which
  60.  * the token was allocated.
  61.  *
  62.  * Leaf classes (that are actually instantiated from the heap) must
  63.  * implement the SizeOf() method, which Destroy() uses to determine
  64.  * the size of the token in order to properly recycle it.
  65.  */
  66.  
  67.  
  68. #ifndef CTOKEN__
  69. #define CTOKEN__
  70.  
  71. #include "prtypes.h"
  72. #include "nsString.h"
  73. #include "nsError.h"
  74. #include "nsFixedSizeAllocator.h"
  75.  
  76. #define NS_HTMLTOKENS_NOT_AN_ENTITY \
  77.   NS_ERROR_GENERATE_SUCCESS(NS_ERROR_MODULE_HTMLPARSER,2000)
  78.  
  79. class nsScanner;
  80. class nsTokenAllocator;
  81.  
  82. enum eContainerInfo {
  83.   eWellFormed,
  84.   eMalformed,
  85.   eFormUnknown
  86. };
  87.  
  88. /**
  89.  * Implement the SizeOf() method; leaf classes derived from CToken
  90.  * must declare this.
  91.  */
  92. #define CTOKEN_IMPL_SIZEOF                                \
  93. protected:                                                \
  94.   virtual size_t SizeOf() const { return sizeof(*this); } \
  95. public:
  96.  
  97. /**
  98.  *  Token objects represent sequences of characters as they
  99.  *  are consumed from the input stream (URL). While they're
  100.  *  pretty general in nature, we use subclasses (found in
  101.  *  nsHTMLTokens.h) to define <start>, </end>, <text>,
  102.  *  <comment>, <&entity>, <newline>, and <whitespace> tokens.
  103.  *  
  104.  *  @update  gess 3/25/98
  105.  */
  106. class CToken {
  107.   public:
  108.  
  109.     enum  eTokenOrigin {eSource,eResidualStyle};
  110.  
  111.   protected:
  112.  
  113.     // nsTokenAllocator should be the only class that tries to
  114.     // allocate tokens from the heap.
  115.     friend class nsTokenAllocator;
  116.  
  117.     /**
  118.      * 
  119.      * @update    harishd 08/01/00
  120.      * @param   aSize    - 
  121.      * @param   aArena   - Allocate memory from this pool.
  122.      */
  123.     static void * operator new (size_t aSize,nsFixedSizeAllocator& anArena) CPP_THROW_NEW
  124.     {
  125.       return anArena.Alloc(aSize);
  126.     }
  127.  
  128.     /**
  129.      * Hide operator delete; clients should use Destroy() instead.
  130.      */
  131.     static void operator delete (void*,size_t) {}
  132.  
  133.   public:
  134.     /**
  135.      * destructor
  136.      * @update    gess5/11/98
  137.      */
  138.     virtual ~CToken();
  139.  
  140.     /**
  141.      * Destroy a token.
  142.      */
  143.     static void Destroy(CToken* aToken,nsFixedSizeAllocator& aArenaPool)
  144.     {
  145.       size_t sz = aToken->SizeOf();
  146.       aToken->~CToken();
  147.       aArenaPool.Free(aToken, sz);
  148.     }
  149.  
  150.     /**
  151.      * Make a note on number of times you have been referenced
  152.      * @update    harishd 08/02/00
  153.      */
  154.     void AddRef() { ++mUseCount; }
  155.     
  156.     /**
  157.      * Free yourself if no one is holding you.
  158.      * @update    harishd 08/02/00
  159.      */
  160.     void Release(nsFixedSizeAllocator& aArenaPool) {
  161.       if(--mUseCount==0)
  162.         Destroy(this, aArenaPool);
  163.     }
  164.  
  165.     /**
  166.      * Default constructor
  167.      * @update    gess7/21/98
  168.      */
  169.     CToken(PRInt32 aTag=0);
  170.  
  171.     /**
  172.      * Retrieve string value of the token
  173.      * @update    gess5/11/98
  174.      * @return  reference to string containing string value
  175.      */
  176.     virtual const nsSubstring& GetStringValue(void) = 0;
  177.  
  178.     /**
  179.      * Get string of full contents, suitable for debug dump.
  180.      * It should look exactly like the input source.
  181.      * @update    gess5/11/98
  182.      * @return  reference to string containing string value
  183.      */
  184.     virtual void GetSource(nsString& anOutputString);
  185.  
  186.     /** @update    harishd 03/23/00
  187.      *  @return  reference to string containing string value
  188.      */
  189.     virtual void AppendSourceTo(nsAString& anOutputString);
  190.  
  191.     /**
  192.      * Sets the ordinal value of this token (not currently used)
  193.      * @update    gess5/11/98
  194.      * @param   value is the new ord value for this token
  195.      */
  196.     void SetTypeID(PRInt32 aValue) {
  197.       mTypeID = aValue;
  198.     }
  199.     
  200.     /**
  201.      * Getter which retrieves the current ordinal value for this token
  202.      * @update    gess5/11/98
  203.      * @return  current ordinal value 
  204.      */
  205.     virtual PRInt32 GetTypeID(void);
  206.  
  207.     /**
  208.      * Getter which retrieves the current attribute count for this token
  209.      * @update    gess5/11/98
  210.      * @return  current attribute count 
  211.      */
  212.     virtual PRInt16 GetAttributeCount(void);
  213.  
  214.     /**
  215.      * Causes token to consume data from given scanner.
  216.      * Note that behavior varies wildly between CToken subclasses.
  217.      * @update    gess5/11/98
  218.      * @param   aChar -- most recent char consumed
  219.      * @param   aScanner -- input source where token should get data
  220.      * @return  error code (0 means ok)
  221.      */
  222.     virtual nsresult Consume(PRUnichar aChar,nsScanner& aScanner,PRInt32 aMode);
  223.  
  224.     /**
  225.      * Getter which retrieves type of token
  226.      * @update    gess5/11/98
  227.      * @return  int containing token type
  228.      */
  229.     virtual PRInt32 GetTokenType(void);
  230.  
  231.     /**
  232.      * For tokens who care, this can tell us whether the token is 
  233.      * well formed or not.
  234.      *
  235.      * @update    gess 8/30/00
  236.      * @return  PR_FALSE; subclasses MUST override if they care.
  237.      */
  238.     virtual PRBool IsWellFormed(void) const {return PR_FALSE;}
  239.  
  240.     virtual PRBool IsEmpty(void) { return PR_FALSE; }
  241.     
  242.     /**
  243.      * If aValue is TRUE then the token represents a short-hand tag
  244.      */
  245.     virtual void SetEmpty(PRBool aValue) { return ; }
  246.  
  247.     PRInt32 GetNewlineCount() 
  248.     { 
  249.       return mNewlineCount; 
  250.     }
  251.  
  252.     void SetNewlineCount(PRInt32 aCount)
  253.     {
  254.       mNewlineCount = aCount;
  255.     }
  256.  
  257.     PRInt32 GetLineNumber() 
  258.     { 
  259.       return mLineNumber;
  260.     }
  261.  
  262.     void SetLineNumber(PRInt32 aLineNumber) 
  263.     { 
  264.       mLineNumber = mLineNumber == 0 ? aLineNumber : mLineNumber;
  265.     }
  266.  
  267.     void SetInError(PRBool aInError)
  268.     {
  269.       mInError = aInError;
  270.     }
  271.  
  272.     PRBool IsInError()
  273.     {
  274.       return mInError;
  275.     }
  276.  
  277.     void SetAttributeCount(PRInt16 aValue) {  mAttrCount = aValue; }
  278.  
  279.     /**
  280.      * perform self test.
  281.      * @update    gess5/11/98
  282.      */
  283.     virtual void SelfTest(void);
  284.  
  285.     static int GetTokenCount();
  286.  
  287.     
  288.  
  289. protected:
  290.     /**
  291.      * Returns the size of the token object.
  292.      */
  293.     virtual size_t SizeOf() const = 0;
  294.  
  295.     PRInt32 mTypeID;
  296.     PRInt32 mUseCount;
  297.     PRInt32 mNewlineCount;
  298.     PRUint32 mLineNumber : 31;
  299.     PRUint32 mInError : 1;
  300.     PRInt16 mAttrCount;
  301. };
  302.  
  303.  
  304.  
  305. #endif
  306.  
  307.  
  308.